package com.pets.springboot_petshop.controller;


import com.pets.springboot_petshop.entity.Service;
import com.pets.springboot_petshop.pojo.Result;
import com.pets.springboot_petshop.service.PettypeService;
import com.pets.springboot_petshop.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/14 下午2:22 周一
 * @Description: 服务控制层
 */
@RestController
@RequestMapping("/Service")
public class ServiceController {
    @Autowired
    private ServiceService serviceService;
    @Autowired
    private PettypeService pettypeService;

    @GetMapping("/selectService")
    private Result selectService(@RequestParam Integer id) {
        Service service = serviceService.selectService(id);
        if (service == null) {
            return Result.error("获取失败");
        }else {
            return Result.success(service);
        }
    }
    //selectPettypeName，通过宠物分类查询
    @GetMapping("/selectPettypeName")
    private Result selectPettypeName(@RequestParam String pettypeName) {
        List<Service> services = serviceService.selectPettypeName(pettypeName);
        if (services == null || services.isEmpty()) {
            // 如果列表为空或为null，返回错误状态
            return Result.error("没有找到相关产品");
        } else {
            // 如果列表不为空，返回产品列表
            return Result.success(services);
        }
    }
    //name，通过姓名模糊查询
    @GetMapping("/selectServicename")
    private Result selectServicename(@RequestParam String name) {
        List<Service> services = serviceService.selectServicename(name);
        if(services == null || services.isEmpty()){
            return Result.error("获取失败");
        }else {
            return Result.success(services);
        }
    }
    //查询所有
    @GetMapping("/findAll")
    private Result findAll() {
        List<Service> services = serviceService.findAll();
        if(services == null || services.isEmpty()){
            return Result.error("获取失败");
        }else {
            return Result.success(services);
        }
    }
    //添加
    @PostMapping("/addService")
    public Result addService(@RequestBody Service service) {
        try {
            int result = serviceService.addService(service);
            if (result > 0) {
                return Result.success("新增成功");
            } else {
                return Result.error("新增失败");
            }
        } catch (Exception e) {
            return Result.error("服务异常：" + e.toString());
        }
    }
    //删除
    @PostMapping("/deleteServiceById")
    private Result deleteServiceById(@RequestParam Integer id) {
        if( serviceService.deleteServiceById(id) > 0){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }
    // 修改数据
    @PostMapping("/updateServiceById")
    public Result updateServiceById(@RequestBody Service service) {
        try {
            // 首先检查传入的Service对象是否为空
            if (service == null) {
                return Result.error("服务信息不能为空");
            }
            // 调用serviceService的updateServiceById方法更新服务信息
            int updateResult = serviceService.updateServiceById(service);
            if (updateResult > 0) {
                // 如果更新的行数大于0，表示更新成功
                return Result.success("修改成功");
            } else {
                // 如果更新的行数不大于0，表示更新失败
                return Result.error("修改失败");
            }
        } catch (Exception e) {
            // 捕获并处理异常，返回错误信息
            return Result.error("服务更新异常：" + e.toString());
        }
    }
}
